
//
//  YDOAApplyChargeVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/6/6.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit


class YDOAApplyChargeVC: UIViewController {

    var myTableView : UITableView?
    lazy var dataSource : Array = Array<YDOAFreeModel>()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        self.title = "物资申请详情"
     
        setupUI()
        
        
        //获取数据
        getData()
    
    }
    
    
    private func getData(){
        let model = YDOAFreeModel()
        dataSource.append(model)
        myTableView?.reloadData()
    }
    

    private func setupUI(){
      
        myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight-120), style: .plain)
        myTableView?.delegate = self
        myTableView?.dataSource = self
       myTableView?.register( UINib(nibName: "YDOAChargeCell", bundle: nil), forCellReuseIdentifier: "YDOAChargeCell")
 
        
        
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 44))
        button.setTitle("再添加一个", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        
        myTableView?.tableFooterView = button
        self.view.addSubview(myTableView!)
        
        
    
    }
    
    
    //确定按钮点击
    @IBAction func sureBtnClick(_ sender: Any) {
        
        let VC  = YDOAChargeDetialVC()
        VC.dataSoure = self.dataSource
        navigationController?.pushViewController(VC, animated: true)
        
    }
  
    
    
    //点击的话载添加一个申请单
    @objc private func btnClick(){
        let model = YDOAFreeModel()
        dataSource.append(model)
        myTableView?.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}


extension YDOAApplyChargeVC : UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return dataSource.count
    }
    
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        guard let cell  = tableView.dequeueReusableCell(withIdentifier: "cell") as? UITableViewCell else {
//            return UITableViewCell()
//        }
//    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAChargeCell", for: indexPath) as? YDOAChargeCell else { return YDOAChargeCell() }
        cell.model = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 220.0
    }
    

    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}


